<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script>
			function check(){
				var $cid=cid.value;
				var xhr= new XMLHttpRequest();
				xhr.open("get",`/shopping/v1/shopping_reg/check/${$cid}`,true);
				xhr.send();
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4 && xhr.status==200){
						var r=xhr.responseText;
						if(r==1){
							alert("编号已存在");
							location.href="shopping_reg.html";
						}
					}
				}
			}
			function show(){
				//获取用户数据
				var _cid=cid.value;
				var _user_id=user_id.value;
				var _product_id=product_id.value;
				var _count=count.value;
				//1.创建xhr异步对象
				var xhr= new XMLHttpRequest();
				//2.创建请求，打开响应
				xhr.open("post",`/shopping/v1/shopping_reg`,true);
				var formdata=`cid=${_cid}&user_id=${_user_id}&product_id=${_product_id}&count=${_count}`;
				xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				//3.发送请求
				xhr.send(formdata);
				//4.创建监听，接收响应
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4 && xhr.status==200){
						var r=xhr.responseText;
						if(r==1){
							d1.innerHTML="添加成功";
						}else{
							d1.innerHTML="添加失败";
						}
					}
				}
			}
		</script>
	</head>
	<body>
		
		<input type="text" id="cid" onblur="check()">编号<br>
		<input type="text" id="user_id">使用者id<br>
		<input type="text" id="product_id">商品id<br>
		<input type="text" id="count">数量<br>
		<button onclick="show()">添加</button>
		<div id="d1"></div>
	</body>
</html>
